package com.huahang.fashionpress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.mapper.CategoryMapper;
import com.huahang.fashionpress.model.Category;
import com.huahang.fashionpress.service.CategoryService;
import com.huahang.fashionpress.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

@Service
@Slf4j
public class CategoryServicelImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMmpper;
    @Autowired
    ProductService productService;
//判断商品类名是否存在。
    @Override
    public boolean checkCategoryByName(String name)  {
        if(name==null||name=="")
        {
            log.info("【类别管理】：查询类别输入name为空。");
            throw new ResultException(ExceptionEnum.DATA_ERROR);
        }
        Category category =categoryMmpper.checkCategoryByName(name);
        if(category == null){
            return false;
        }
        else{
            return true;
        }
    }
    //查找所有商品类信息
    @Override
    public List<Category> SelectallCategory() {
        List<Category> categories = categoryMmpper.selectAllCategory();
        return categories;
    }

    @Override
    public Category SelectCategoryById(int id) {
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id)) || id < 0){
            log.info("【类别管理】：删除类别ID为空");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        return categoryMmpper.selectById(id);
    }

    @Override
    public Category SelectCategoryByName(String name) {
        if(name==null||name=="")
        {
            log.info("【类别管理】：查询类别输入name为空。");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        Category category = categoryMmpper.checkCategoryByName(name);
        return category;
    }


    //插入商品
    @Override
    public void insertCategory(Category category) {
        if(category == null||category.equals("")){
            log.info("【类别管理】：插入类别信息为空");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        //商品类型名重复会报错，不重复才会插入
        if(checkCategoryByName(category.getCategoryName()) ){
            log.info("【类别管理】：插入类别信息重复");
            throw new ResultException(ExceptionEnum.RE_ERROR);
        }
        categoryMmpper.insert(category);


    }
    //更新商品信息
    @Transactional
    @Override
    public void updateCategory(Category category) {
        if(category == null||category.equals("")){
            log.info("【类别管理】：更新信息为空");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        categoryMmpper.updateById(category);
    }

    //根据商品ID删除
    @Override
    public void delteCategory(int id) {
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id)) || id < 0){
            log.info("【类别管理】：删除类别ID为空");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        //该商品类别下有商品
        if(productService.SelectProductByCategoryId(id).size() !=0 ){
            log.info("【类别管理】：该类别下有商品存在");
            throw new ResultException(ExceptionEnum.LATER_ERROR);
        }
        categoryMmpper.deleteById(id);
    }


}
